$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/product/list',
        datatype: "json",
        colModel: [			
            { label: '产品名称', name: 'name', index: 'name', width: 80 },
            { label: '开票名称', name: 'invoiceName', index: 'invoice_name', width: 80 },
            { label: '产品编号', name: 'code', index: 'code', width: 80 },
            { label: '供应商', name: 'supplierId', index: 'supplier_id', width: 0,hidden:true },
            { label: '供应商', name: 'supplierName', index: 'supplier_name', width: 80 },
			{ label: '产品分类', name: 'cateId', index: 'cate_id', width: 0 ,hidden:true},
			{ label: '产品分类', name: 'categoryName', index: 'category_name', width: 80 },
			{ label: '材质', name: 'material', index: 'material', width: 80 },
			{ label: '型号', name: 'model', index: 'model', width: 80 }, 			
			{ label: '数量', name: 'number', index: 'number', width: 80 , formatter: function(value, options, row){
                    return value >1000 ?
                        '<span class="label label-success">'+value+'</span>' :
                        '<span class="label label-warning">'+value+'</span>';
                }},
			{ label: '单位', name: 'unit', index: 'unit', width: 80 },
			// { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
			// { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 },
			// { label: '是否已删除', name: 'isDelete', index: 'is_delete', width: 80 },
            { label: '状态', name: 'isDelete', width: 60, formatter: function(value, options, row){
                    return value === 1 ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }}
			// { label: '备注', name: 'remark', index: 'remark', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            keyword: null
        },
		showList: 1,
		title: null,
		product: {},
		purchase: {},
		categoryList:[],
		supplierList:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = 2;
			vm.title = "新增";
			vm.product = {
                isDelete:0
            };
            vm.categories();
            vm.supplier();
		},
		purchaseIn: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.purchase={};
            vm.purchase.proId=id;
			vm.showList = 3;
			vm.title = "进货";
            $.get(baseURL + "sys/product/info/"+id, function(r){
                vm.product = r.product;
            });
		},
		purchaseInSave: function(){
            vm.purchase.name=vm.product.name;
            vm.purchase.cateName=vm.product.categoryName;
            vm.purchase.supplierName=vm.product.supplierName;
            vm.purchase.material=vm.product.material;
            vm.purchase.model=vm.product.model;
            vm.purchase.size=vm.product.size;
            vm.purchase.code=vm.product.code;

            $.ajax({
                type: "POST",
                url: baseURL + "sys/purchase/save",
                contentType: "application/json",
                data: JSON.stringify(vm.purchase),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = 2;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			var url = vm.product.id == null ? "sys/product/save" : "sys/product/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.product),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/product/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/product/info/"+id, function(r){
                vm.product = r.product;
            });
            vm.categories();
            vm.supplier();
		},
		reload: function (event) {

			vm.showList = 1;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'keyword': vm.q.keyword},
                page:page
            }).trigger("reloadGrid");
		},
		categories: function () {
            $.get(baseURL + "sys/productcategory/list", function(r){
                vm.categoryList = r.page.list;
            });
        },
		supplier: function () {
            $.get(baseURL + "sys/supplier/purchasers?type=1", function(r){
                vm.supplierList = r.data;
            });
        }
	}
});